package com.burgeon.backend.EnterpriseWeChat.rest.Gtasks;

import com.burgeon.backend.EnterpriseWeChat.entity.FittingVip;
import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class GetFittingVip extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetFittingVip(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<FittingVip> getData(String empId,String returnState) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,returnState), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
//        HashMap map = new HashMap();
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<FittingVip> vips= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length() ; i++) {
                FittingVip vip = new FittingVip();
                vip.setReceiverName(jsonArray.getJSONArray(i).getString(0));
                vip.setEmpId(jsonArray.getJSONArray(i).getString(1));
                vip.setPhone(jsonArray.getJSONArray(i).getString(2));
                vip.setCardNo(jsonArray.getJSONArray(i).getString(3));
                vip.setAddress(jsonArray.getJSONArray(i).getString(4));
                vip.setDateEnd(jsonArray.getJSONArray(i).getString(5));
                vip.setDateEnd1(jsonArray.getJSONArray(i).getString(6));
                vip.setStoreName(jsonArray.getJSONArray(i).getString(7));
                vip.setEmpName(jsonArray.getJSONArray(i).getString(8));
                vip.setDocNO(jsonArray.getJSONArray(i).getString(9));
                vip.setBillDate(jsonArray.getJSONArray(i).getString(10));
                vip.setVipId(jsonArray.getJSONArray(i).getString(11));

                vip.setFittingState(jsonArray.getJSONArray(i).getString(12));
                vip.setFittingId(jsonArray.getJSONArray(i).getString(13));
                vip.setOpenid(jsonArray.getJSONArray(i).getString(14));
                if("null".equals(jsonArray.getJSONArray(i).getString(15))){
                    vip.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
                }else {
                    vip.setLogoImg(jsonArray.getJSONArray(i).getString(15));
                }

                if("1".equals(returnState)){
                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
                    Date firstDate= new Date();
                    Date secondDate=null;
                    try {
//                    firstDate = df.parse(firstDate);
                        secondDate=df.parse(vip.getDateEnd());
                    }
                    catch(Exception e) {
                        // 日期型字符串格式错误
                    }
                    assert secondDate != null;
                    int nDay=(int)((secondDate.getTime()-firstDate.getTime())/(24*60*60*1000));
                    vip.setRemainingTime(nDay);
                }


                vips.add(vip);
            }
        }


        return vips;
    }

    private MultiValueMap Data(String empId,String returnState) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("RECEIVER_NAME");//
        colums.add("'C_VIP_ID;HR_EMPLOYEE_ID1;ID'");
        colums.add("'C_VIP_ID;MOBIL'");
        colums.add("'C_VIP_ID;CARDNO'");

        colums.add("ADDRESS");//收货地址
        colums.add("DATEEND");//预计归还日期
        colums.add("DATEEND1");//实际归还日期
        colums.add("'C_STORE_ID;NAME'");
        colums.add("'HR_EMPLOYEE_ID;NAME'");
        colums.add("DOCNO");
        colums.add("BILLDATE");
        colums.add("C_VIP_ID");
        colums.add("FITTING_START");
        colums.add("ID");
        colums.add("'C_VIP_ID;OPENID2'");

        colums.add("'C_VIP_ID;LOGO_URL'");

        params.put("columns", colums);
        params.put("table", "M_FITTING");//表名
        params.put("count", "true");

        HashMap orderby = new HashMap();

        orderby.put("column", "BILLDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr11 = new HashMap();
        HashMap expr12 = new HashMap();

        expr11.put("column","'HR_EMPLOYEE_ID'");
        String ac = "'"+empId+"'";
        expr11.put("condition",ac);

        expr12.put("column","'FITTING_START'");
        String b = "'"+returnState+"'";
        expr12.put("condition",b);

        expr1.put("combine", "and");
        expr1.put("expr1", expr11);
        expr1.put("expr2", expr12);

        HashMap expr2 = new HashMap();
        HashMap expr21 = new HashMap();

        expr21.put("column","'STATUS'");
        String c = "'2'";
        expr21.put("condition",c);

        expr2.put("combine", "and");
        expr2.put("expr1", expr21);
        expr2.put("expr2", expr21);

        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetFittingVip g = new GetFittingVip("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("6270","1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
